package com.java.simple.study.juc.p5;

import com.java.simple.study.tool.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhoujunlin
 * @date 2022年06月28日 21:56
 * @desc
 */
@Slf4j(topic = "c.SlowMotion")
public class SlowMotion {

    public static void main(String[] args) {
        AtomicInteger balance = new AtomicInteger(10000);
        int mainPrev = balance.get();
        log.debug("try get {}", mainPrev);

        new Thread(() -> {
            Sleeper.sleep(1);
            int prev = balance.get();
            balance.compareAndSet(prev, 9000);
            log.debug(balance.toString());
        }, "t1").start();

        Sleeper.sleep(2);
        log.debug("try set 8000...");
        boolean success = balance.compareAndSet(mainPrev, 8000);
        log.debug("is success? {}", success);
        if (!success) {
            mainPrev = balance.get();
            log.debug("try set 8000...");
            success = balance.compareAndSet(mainPrev, 8000);
            log.debug("is success? {}", success);
        }

    }

}
